/* eslint-disable */
import React from 'react';
import { Router } from 'dva/router';
import { map, isArray, isString } from 'lodash';

export default function ({ history, app }) {
    const startPaths = [
        'start/login',
        'start/find-pw',
        'start/register'
    ];
    const makeChildRoutes = (data = [], componentDir = './routes/', modelDir = './models/') => {
        const endLine = /\/$/;
        return map(data, (d) => {
            let path;
            let loadModel = 0;
            let component = null;
            if (isArray(d)) {
                [path, loadModel = 0, component] = d;
            } else if (isString(d)) {
                path = d;
            } else {
                throw new Error('子路由配置项的类型必须是字符串或者数组');
            }
            let componentPath = `${componentDir}${component || path}`;
            if (endLine.test(path)) {
                path = path.replace(endLine, '');
                componentPath = `${componentDir}${component || path}/index`;
            }
            let modelPath;
            if (loadModel) {
                const mp = path.match(/\w*$/)[0];
                modelPath = `${modelDir}${loadModel === 1 ? mp : loadModel}`;
            }
            return {
                path,
                getComponent(nextState, cb) {
                    require.ensure([], require => {
                        if (modelPath && loadModel !== 0) app.modelX(require(`${modelPath}`));
                        cb(null, require(`${componentPath}`));
                    });
                },
            };
        })
    };
    const routes = [
        {
            path: '/',
            getIndexRoute(nextState, cb) {
                require.ensure([], require => {
                    cb(null, { component: require('./start/login') });
                });
            },
            getComponent(nextState, cb) {
                require.ensure([], require => {
                    cb(null, require('./start'));
                });
            },
            childRoutes: makeChildRoutes(startPaths, './'),
        },
        {
            path: 'store',
            getIndexRoute(nextState, cb) {
                require.ensure([], require => {
                    cb(null, { component: require('./storeSystem/routes/store') });
                });
            },
            getComponent(nextState, cb) {
                require.ensure([], require => {
                    app.modelX(require('./storeSystem/models/store'));
                    cb(null, require('./home'));
                });
            },
            childRoutes: [
                {
                    path: 'info',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            cb(null, require('./storeSystem/routes/store'));
                        });
                    },
                },
                {
                    path: 'employee',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/employee'));
                            cb(null, require('./storeSystem/routes/employee'));
                        });
                    },
                },
                {
                    path: 'device',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/device'));
                            cb(null, require('./storeSystem/routes/device'));
                        });
                    },
                },
                {
                    path: 'game',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/game'));
                            cb(null, require('./storeSystem/routes/game'));
                        });
                    },
                },
            ],
        },
        {
            path: 'marketing',
            getIndexRoute(nextState, cb) {
                require.ensure([], require => {
                    cb(null, { component: require('./storeSystem/routes/event') });
                });
            },
            getComponent(nextState, cb) {
                require.ensure([], require => {
                    app.modelX(require('./storeSystem/models/event'));
                    cb(null, require('./home'));
                });
            },
            childRoutes: [
                {
                    path: 'topUp',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            cb(null, require('./storeSystem/routes/event'));
                        });
                    },
                },
                {
                    path: 'package',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/package'));
                            cb(null, require('./storeSystem/routes/package'));
                        });
                    },
                },
            ],
        },
        {
            path: 'financial',
            getIndexRoute(nextState, cb) {
                require.ensure([], require => {
                    cb(null, { component: require('./storeSystem/routes/financial/orders') });
                });
            },
            getComponent(nextState, cb) {
                require.ensure([], require => {
                    app.modelX(require('./storeSystem/models/orders'));
                    cb(null, require('./home'));
                });
            },
            childRoutes: [
                {
                    path: 'orders',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/orders'));
                            cb(null, require('./storeSystem/routes/financial/orders'));
                        });
                    },
                },
                {
                    path: 'income',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/income'));
                            cb(null, require('./storeSystem/routes/financial/income'));
                        });
                    },
                },
                {
                    path: 'cards',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/cards'));
                            cb(null, require('./storeSystem/routes/financial/cards'));
                        });
                    },
                },
                {
                    path: 'card',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/card'));
                            cb(null, require('./storeSystem/routes/financial/card'));
                        });
                    },
                },
            ],
        },
        {
            path: 'analysis',
            getIndexRoute(nextState, cb) {
                require.ensure([], require => {
                    cb(null, { component: require('./storeSystem/routes/analysis/games') });
                });
            },
            getComponent(nextState, cb) {
                require.ensure([], require => {
                    app.modelX(require('./storeSystem/models/operateGames'));
                    cb(null, require('./home'));
                });
            },
            childRoutes: [
                {
                    path: 'games',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            cb(null, require('./storeSystem/routes/analysis/games'));
                        });
                    },
                },
                {
                    path: 'devices',
                    getComponent(nextState, cb) {
                        require.ensure([], require => {
                            app.modelX(require('./storeSystem/models/operateDevices'));
                            cb(null, require('./storeSystem/routes/analysis/devices'));
                        });
                    },
                },
            ],
        }
    ];
    return (<Router history={history} routes={routes} />);
}
